/*
 * BLLib.Net.Packet
 * Andy Tidball
 * 
 * Project: Black Lightning General Library
 * Copyright: GNU General Public License
 */

using System;

namespace BLLib.Net {
	/// <summary>
	/// Abstract base class for packets to be sent or received through a PacketConnection.
	/// </summary>
	[Serializable]
	public abstract class Packet {
		// member data
		private string   _Data;         // the data string contained in the packet
		private DateTime _CreationTime; // the date and time that the packet was created

		/// <summary>
		/// Creates a new Packet.
		/// </summary>
		/// <param name="Data">The data string contained in the packet.</param>
		/// <param name="Context">Contextual information to use to parse information from the given data.</param>
		public Packet(string Data, PacketContext Context) {
			_Data         = Data;
			_CreationTime = DateTime.Now;
		}

		/// <summary>
		/// Copy constructor.
		/// </summary>
		/// <param name="P">The packet to create a copy of.</param>
		public Packet(Packet P) {
			_Data         = P._Data;
			_CreationTime = P._CreationTime;
		}

		/// <summary>
		/// Gets the data string that the packet was created with.
		/// </summary>
		public string RawInput {
			get {
				return _Data;
			}
		}

		/// <summary>
		/// The date and time that this packet was created.
		/// </summary>
		public DateTime CreationTime {
			get {
				return _CreationTime;
			}
		}

		/// <summary>
		/// Constructs an output raw data string from the current state of whatever constructs are used by the derived implementation.
		/// </summary>
		/// <param name="Context">Contextual information to use to create output from the packet's data.</param>
		/// <returns>A data string that reflects the current state of the derived packet.</returns>
		public abstract string CreateOutput(PacketContext Context);
	}

	/// <summary>
	/// A packet which doesn't break the data string down into any more detail.
	/// </summary>
	[Serializable]
	public class RawPacket : Packet {
		// member data
		private string _Data; // the packet's data

		/// <summary>
		/// Creates a new RawPacket.
		/// </summary>
		/// <param name="Data">The data to contain in the packet.</param>
		/// <param name="Context">Contextual information to use to parse information from the given data.</param>
		public RawPacket(string Data, PacketContext Context)
			: base(Data, Context) {
		}

		/// <summary>
		/// The data contained in the packet.
		/// </summary>
		public string Data {
			get {
				return _Data;
			}
			set {
				_Data = value;
			}
		}

		/// <summary>
		/// Retrieves the packet's data.
		/// </summary>
		/// <param name="Context">Contextual information to use to create output from the packet's data.</param>
		/// <returns>The packet's data string.</returns>
		public override string CreateOutput(PacketContext Context) {
			return _Data;
		}
	}

	/// <summary>
	/// The contextual information passed to packets when they're parsed into data and re-formed into output.
	/// </summary>
	public class PacketContext {
	}
}
